package com.huawei.java.tools.csvreaders;

import com.huawei.java.pojo.EdgeNode;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.PriorityQueue;

/**
 * @author henrydai
 * @version 1.0
 * @date 2022/3/15 19:33
 * @description
 */
public class SiteBandwidthReader {

    /**
     * 优先级队列 读取site_bandwidth文件, 将边缘节点信息添加到优先级队列中
     * @param dir
     * @return
     */
    public void readFile(String dir, PriorityQueue<EdgeNode> edgeNodesPriorityQueue)
    {
        Path path = Paths.get(dir);
        try {
            BufferedReader br = Files.newBufferedReader(path);
            //读取标题栏
            String s = br.readLine();
            //下面读取正文
            s = br.readLine();
            while(s!=null)
            {
                String[] nameAndCapability = s.split(",");
                EdgeNode edgeNode = new EdgeNode(nameAndCapability[0], Integer.parseInt(nameAndCapability[1]));
                edgeNodesPriorityQueue.add(edgeNode);
                s = br.readLine();
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 数组读取, 读取site_bandwidth文件, 将边缘节点信息添加到优先级队列中
     * @param dir
     * @return
     */
    public void readFile(String dir, ArrayList<EdgeNode> list)
    {
        Path path = Paths.get(dir);
        try {
            BufferedReader br = Files.newBufferedReader(path);
            //读取标题栏
            String s = br.readLine();
            //下面读取正文
            s = br.readLine();
            while(s!=null)
            {
                String[] nameAndCapability = s.split(",");
                EdgeNode edgeNode = new EdgeNode(nameAndCapability[0], Integer.parseInt(nameAndCapability[1]));
                list.add(edgeNode);
                s = br.readLine();
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
